'use strict';
var moment = require('moment');

/**
 * add of sales
 */
exports.add = function(req, res) {
    var data = {};
    data=req.body;
    data['month']=moment(data['month']).format('YYYY-MM-DD HH:mm:ss');
    if(JSON.stringify(data) != "{}"){
        var sql="(";
        var lastObj="";
        for (var prop in data){
            lastObj=prop;
        }
        for (var prop in data)
        {
            sql+=prop;
            if(prop!=lastObj){
                sql+=" , ";
            }else{
                sql+=") values (";
            }
        }
        for (var prop in data)
        {
            sql+="'"+data[prop]+"' ";
            if(prop!=lastObj){
                sql+=" , ";
            }else{
                sql+=") ";
            }
        }
        req.getConnection(function(err, conn) {
            if (err) {
                return res.send(401);
            } else {
                conn.query('insert into month_sales_planning '+sql, function(err,result) {
                    if (err) {
                        res.json(false);
                    } else {
                        res.json(true); 
                    }
                });
            }
        });
    }else{
        res.json(false);
    }
};

// 获取销售计划列表
exports.search = function(req, res) {
    var data = {};
    data=req.body;
    var sql="";
    if(JSON.stringify(data) != "{}"){
        if(data['month']!=null){
            data['month']=moment(data['month']).format('YYYY-MM-DD HH:mm:ss');
        }
        sql="where ";
        var lastObj="";
        for (var prop in data){
            lastObj=prop;
        }
        for (var prop in data)
        {
            sql+=" "+prop+"='"+data[prop]+"' ";
            if(prop!=lastObj){
                sql+=" AND ";
            }
        }
    }
    req.getConnection(function(err, conn) {
        if (err) {
            return res.send(401);
        } else {
            var userList=[];
            var companyList=[];
            conn.query('select * from company ', function(err,result) {
                companyList=result;
                conn.query('select * from month_sales_planning '+sql, function(err,result) {
                    if (err) {
                        return next(err);
                    } else {
                        for (var i = 0; i < result.length; i++) {
                            for (var j = 0; j < companyList.length; j++) {
                                if(companyList[j].id==result[i].branch_company_id){
                                    result[i].branch_company=companyList[j].company_name;
                                }
                            }
                        }
                        res.json(result);
                    }
                });
            });
        }
    });
};

/**
 * set sales
 */
exports.set = function(req, res) {
    var data = {};
    data=req.body;
    data['month']=moment(data['month']).format('YYYY-MM-DD HH:mm:ss');
    var sql="";
    if(data!={}){
        var lastObj="";
        for (var prop in data){
            if(prop!="id"){
                lastObj=prop;
            }
        }
        for (var prop in data)
        {
            if(prop!="id"){
                sql+=" "+prop+"='"+data[prop]+"' ";
                if(prop!=lastObj){
                    sql+=" , ";
                }
            }
        }
        sql+="where id="+data['id'];
    }
    req.getConnection(function(err, conn) {
        if (err) {
            return res.send(401);
        } else {
            conn.query('UPDATE month_sales_planning SET '+sql, function(err,result) {
                if (err) {
                    res.json(false);
                } else {
                    res.json(true);
                }
            });
        }
    });
};

/**
 * del sales
 */
exports.del = function(req, res) {
    var id=req.body.id;

    req.getConnection(function(err, conn) {
        if (err) {
            return res.send(401);
        } else {
            // console.log(req.body);
            conn.query('DELETE FROM month_sales_planning WHERE id = '+id, function(err,result) {
                if (err) {
                    res.json(false);
                } else {
                    res.json(true); 
                }
            });
        }
    });
};